package util;

import models.Company;
import models.Product;
import models.Types;

public class CheckInfo {

	/**
	 * 检查是否为新增的厂商|类型|产品信息，新的则增加。旧的不处理。
	 * @param company
	 * @param types
	 * @param product
	 * @return
	 */
	public static Product saveOrUpdateCompanyTypeProduct(String company, String types, String product) {
		Company sourceCompany = Company.find("name = ? ", company).first();

		Company targetCompany = null;

		if (null == sourceCompany) {
			targetCompany = new Company();
			targetCompany.name=company.trim();
			targetCompany.save();
		} else {
			targetCompany = sourceCompany;
		}

		Types sourceTypes = Types.find("name = ?", types).first();

		Types targetTypes = null;

		if (null == sourceTypes) {
			targetTypes = new Types();
			targetTypes.name=types.trim();
			targetTypes.company=targetCompany;
			targetTypes.save();
		} else {
			targetTypes = sourceTypes;
		}
		Product sourceProduct = Product.find("name = ?", product).first();

		Product targetProduct = null;

		if (null == sourceProduct) {
			targetProduct = new Product();
			targetProduct.name=product;
			targetProduct.company=targetCompany;
			targetProduct.types=targetTypes;
			targetProduct.save();
		} else {
			targetProduct = sourceProduct;
		}
		return targetProduct;
	}
}
